package com.wz.config;

import com.wz.pojo.USDT;
import org.web3j.abi.datatypes.Address;
import org.web3j.abi.datatypes.generated.Uint256;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.RemoteCall;
import org.web3j.protocol.http.HttpService;
import org.web3j.tx.ClientTransactionManager;
import org.web3j.tx.Contract;
import rx.Observable;

import java.math.BigInteger;

public class USDTapi {

    String url = "http://localhost:8545";
    String fromAddr = "0x8737C46e24Aa01d40C11422B3F31742CF4b0C698";
    String contractAddr = "0x1F321065E878AA4207eD2Ce614e7ab9a7025b6EE";
    String _owner = "0x19662Fe14475bB2975034B1818d52C8d5f06Cdab";
    public USDTapi() {
    }

    public USDTapi(String url, String fromAddr, String contractAddr) {
        this.url = url;
        this.fromAddr = fromAddr;
        this.contractAddr = contractAddr;
    }

    public static class TransferEventResponse {
        public Address _from;

        public Address _to;

        public Uint256 _value;

        public String _transactionHash;
    }


    public USDTapi(String _owner) {
        this._owner = _owner;
    }

    Web3j web3j = Web3j.build(new HttpService(url));
    ClientTransactionManager transactionManager = new ClientTransactionManager(web3j,
            fromAddr);
    //token是智能合约的实例对象
    USDT token = USDT.load(contractAddr, web3j, transactionManager,
            Contract.GAS_PRICE, Contract.GAS_LIMIT);

    public void USDTGetBalance() {
        RemoteCall<BigInteger> balance = token.balanceOf(_owner);
        try {
            BigInteger bigInteger = balance.send();
            System.out.println(bigInteger);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    public Observable<USDT.TransferEventResponse>  transferEventOb(){
        
        Observable<USDT.TransferEventResponse> event = token.transferEventObservable(
                DefaultBlockParameterName.EARLIEST, DefaultBlockParameterName.LATEST);
//        event.subscribe(tx -> {
//
//        });
        return event;
    }

    

}
